const app = getApp()

import { HiddenCall } from '../../request/api'
const request = new HiddenCall
import filter from '../../mixins/filter'
Page({
  /**
  *  通信录详情
  * - @author           王磊
  * - @date             2018-07-13
  * headerList         通信录详情tab
  * isType               通信录详情tab下标
  */
  mixins: [filter],
  data: {
    headerList: [
      { name: "通话" }, { name: "短信" }
    ],
    isType: 0,
    page: 1
  },
  onLoad: function (options) {
    // 判断是不是短信详情
    if (options.model) {
      let model = JSON.parse(options.model)
      // 获取进入电话||详情类型
      this.setData({
        showPage: model.callType, // 显示类型
        detailType: model.detailType,
        sendTel: model.sendTel ? model.sendTel : '', // 房客源列表传进的数据
        callModel: model.callModel ? model.callModel:'', // 所有列表带有房客源进来的数据
        model: model
      })
      // 是否为短信，改变页面标题
      this.runSms(model)
    }
    // 调用通信录详情列表
    // this.maillingDetail()
    let modelRecord = {
      "customerPhone": this.data.sendTel.realNumber ? this.data.sendTel.realNumber : this.data.callModel.customerPhone ? this.data.callModel.customerPhone : this.data.model.sendTel,
      "page": this.data.page,
      "pageSize": 5,
      "resourceCode": this.data.model.code
    }
    this.smsRecording(modelRecord)
    this.sendDetail()
  },
  /**
   * 切换通话||短信
   * author wl
   */
  writeDescription(e) {
    this.setData({
      isType: e.currentTarget.dataset.istype
    })
  },
  /**
   * 查询通信录详情列表
   * author wl
   */
  maillingDetail: function () {
    request.maillingDetail().then((res) => {
      this.setData({ detailList: res.data.result })
    })
  },
  /**
   * 获取短信发送内容
   * author wl
   */
  sendValue(e) {
    if (!this.data.content) {
      wx.showToast({
        title: '请选择短信模板！',
        icon: 'none'
      })
      return
    }
    // 请求后端接口，传递出接
    let data = {
      "customerPhone": this.data.sendTel.realNumber ? this.data.sendTel.realNumber : this.data.callModel.customerPhone ? this.data.callModel.customerPhone : this.data.model.sendTel,
      "smsRecord": this.data.content,
      "utype": this.data.queryData.utype,
      "resourceCode": this.data.model.code
    }
    request.sendValue(data).then((res) => {
      if (res.statusCode === 2) {
        wx.showToast({ title: '发送失败', icon: 'none' })
      } else if (res.statusCode === 3) {
        wx.showToast({ title: '短信字数过多！', icon: 'none' })
      } else {
        wx.showToast({ title: '今日发送短信条数已达上线！', icon: 'none' })
      }
      if (res.isSuccess) {
        wx.showToast({ title: '发送成功！', icon: 'none' })
        this.setData({ content: '',sms: "",page: 1 })
        let modelRecord = {
          "customerPhone": this.data.sendTel.realNumber ? this.data.sendTel.realNumber : this.data.callModel.customerPhone ? this.data.callModel.customerPhone : this.data.model.sendTel,
          "page": this.data.page,
          "pageSize": 5,
          "resourceCode": this.data.model.code
        }
        setTimeout(() => {
          this.smsRecording(modelRecord)
        }, 1000)
      } else {
        this.setData({ content: '', sms: "" })
      }
    })
  },
  /**
   * 打开选择理由弹窗
   * @author wl
   */
  openTemplate() {
    this.setData({ isTariff: true })
  },
  /**
   * 关闭选择理由弹窗
   * @author wl
   */
  closeTemplate() {
    this.setData({ isTariff: false })
  },
  /**
   * 获取发送理由
   * @author wl
   */
  getContent(e) {
    let content = e.currentTarget.dataset.content
    let id = e.currentTarget.dataset.id
    if (content) {
      request.smsContent(id).then((res) => {
        let sms = res.content
        sms.replace("由于：", `由于：${content}`)
        this.setData({ content, sms })
      })
      this.setData({ isTariff: false })
    }
  },
  /**
   * 根据用户想发送的短信信息查询
   * @author wl
   */
  sendDetail() {
    // 获取虚拟号
    request.messageNum(this.data.model.code).then(res => {
     res.forEach((el) => {
       if (el.realNumber === this.data.model.sendTel) {
         this.setData({ smsName: el.userName})
         return
       }
     })
    })
  },
  /**
   *  获取模板内容
   *  @author wl
   */
  getTemplate(data) {
    request.getTemplate(data).then((res) => {
      this.setData({ template: res })
    })
  },
  /**
   * 清空选择的模板数据
   * @author wl
   */
  clearContent() {
    this.setData({ content: '', sms: '' })
  },
  /**
   * 是否为短信
   * @author wl
   */
  runSms(model) {
    if (model.callType === "sms") {
      wx.setNavigationBarTitle({ title: "短信" })
      // 获取发短信的详细信息
      // 房客源详情进入
      this.getTemplate(2)
    }
  },
  /**
   * 短信记录
   * @author wl
   */
  smsRecording (model) {
    request.queryMessage(model).then(res => {
      this.setData({
        queryData: res,
        smsDetail: res.recordingModels.content,
        count: res.recordingModels.count
      })
    })
  },
  /**
 * 电话详情上拉加载数据
 * author dzl
 */
  onReachBottom() {
    // 设置加载页数
    this.data.page += 1
      if (this.data.smsDetail.length === this.data.count) {
        return
      }
      let model = {
        "customerPhone": this.data.sendTel.realNumber ? this.data.sendTel.realNumber : this.data.callModel.customerPhone ? this.data.callModel.customerPhone : this.data.model.sendTel,
        "page": this.data.page,
        "pageSize": 5,
        "resourceCode": this.data.model.code
      }
      request.queryMessage(model).then(res => {
        this.data.smsDetail = [...this.data.smsDetail, ...res.recordingModels.content]
        this.setData({
          smsDetail: this.data.smsDetail,
          count: res.recordingModels.count
        })
      })
    }
})
